import unittest
from selenium import webdriver
import time
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By


class jiuxian2(unittest.TestCase):

    def setUp(self):
        # 打开edge浏览器
        self.driver = webdriver.Edge()
        #  打开酒仙网
        self.driver.get('https://www.jiuxian.com/')
        # 窗口最大化
        self.driver.maximize_window()

    def tearDown(self):
        # 关闭浏览器
        self.driver.close()
        print('结束')

    def test_01(self):
        # 搜索框输入五粮液,键盘事件回车
        self.driver.find_element(By.ID, 'wd').send_keys('五粮液', Keys.ENTER)
        time.sleep(2)
        # 选择酒品
        self.driver.find_element(By.XPATH, '/html/body/div[6]/div/div[2]/div[5]/ul/li[7]/div[2]/div[1]/a/img').click()
        time.sleep(2)
        # 切窗口
        self.driver.switch_to.window(self.driver.window_handles[-1])
        time.sleep(2)
        # 断言商品价格是否一样
        dy2 = self.driver.find_element(By.XPATH, '//*[@id="nowPrice"]/span[1]/strong').text
        self.assertEqual('39.00', dy2)
        # 点击加入购物车
        self.driver.find_element(By.NAME, 'detail_jiarugouwuche').click()
        time.sleep(2)
        # 查看购物车
        self.driver.find_element(By.NAME, 'detail_chakangouwuche').click()
        time.sleep(2)
        # 去结算
        self.driver.find_element(By.CLASS_NAME, 'done').click()
        time.sleep(2)
